//
//  HubuNewsViewController.m
//  iHubu2
//
//  Created by hanshui on 12-11-1.
//  Copyright (c) 2012年 hanshui. All rights reserved.
//

#import "HubuNewsViewController.h"
#import "ASIHTTPRequest.h"
#import "OPACConstant.h"
#import "RegexKitLite.h"

@interface HubuNewsViewController ()

@end

@implementation HubuNewsViewController
@synthesize newsWebView;
@synthesize newsId1, newsTitle1,waitingView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
//    [newsWebView loadHTMLString:@"china" baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void) showNewsInfo:(NSString *) newsId newTitle:(NSString *)newsTitle {
    if(![self isViewLoaded]){
        [self loadView];
    }
    
    self.newsId1 = newsId;
    self.newsTitle1 = newsTitle;
    /*
    NSString *resourcePath = [ [NSBundle mainBundle] resourcePath];
    NSString *filePath = [resourcePath stringByAppendingPathComponent:[[NSString alloc] initWithFormat:@"test%d.html", categoryId]];
    NSString *htmlstring=[[NSString alloc] initWithContentsOfFile:filePath  encoding:NSUTF8StringEncoding error:nil];
    [webView loadHTMLString:htmlstring  baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
     */
    
    //获取最新的新闻的内容
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", NEWS_ROOT, newsId]];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    
    [request setDidFinishSelector:@selector(requestCompleted:)];
    [request setDidFailSelector:@selector(requestError:)];
    
    [request setDelegate:self];
    [request startAsynchronous];
}


- (void)requestCompleted:(ASIHTTPRequest *)request
{
    //    NSMutableData *webData = [[NSMutableData alloc] init];
    //    [webData appendData:[request responseData]];
    //    NSString *theXML = [[NSString alloc] initWithBytes:[webData mutableBytes]
    //												length:[webData length]
    //											  encoding:NSASCIIStringEncoding];
    //        NSLog(@"webData:%@", theXML);
    //    NSLog(@"webData2:%@", [OPACViewController replaceUnicode:theXML]);
    //    NSString *responseString = [[request responseString] dataUsingEncoding:NSUTF8StringEncoding];
    //    NSLog(@"responseString:%@", responseString);
    //[self clearHTMLData2Array:theXML];
    
    NSString *responseString = [request responseString];
    //NSLog(@"news:%@", responseString);
    [self clearHTMLData2Array:responseString];
}

- (void)requestError:(ASIHTTPRequest *)request {
    NSError *error = [request error];
    NSLog(@"error:%@", error);
    [waitingView stopAnimating];
}


- (void) clearHTMLData2Array:(NSString *) sourceCode {
    //获取每一本图书信息
    NSRange listNewsBegin = [sourceCode rangeOfString:NEWS_CONTENT_BEGIN];
    if ((listNewsBegin.location != NSNotFound)) {
        NSRange listNewsEnd = [sourceCode rangeOfString:NEWS_CONTENT_END options:nil range:NSMakeRange(listNewsBegin.location, sourceCode.length - listNewsBegin.location)];
        if(listNewsEnd.location != NSNotFound) {
            NSString *content = [sourceCode substringWithRange:NSMakeRange(listNewsBegin.location + listNewsBegin.length, listNewsEnd.location - (listNewsBegin.location + listNewsBegin.length))];
            content = [content stringByReplacingOccurrencesOfString:@"/UserFiles/" withString:[NSString stringWithFormat:@"%@UserFiles/", NEWS_ROOT]];
            content = [content stringByReplacingOccurrencesOfRegex:@"height=\"[0-9]{3,4}\"" withString:@""];
            content = [content stringByReplacingOccurrencesOfRegex:@"width=\"[0-9]{3,4}\"" withString:@"width=\"300\""];
            
            NSString *htmlString = [NSString stringWithFormat:@"<html><body><div align='center' style='font-size:14pt'><br />%@<br /><br /></div><div>%@</div></body><html>", self.newsTitle1, content];
            
            [newsWebView loadHTMLString:htmlString  baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
        }
    }
    [waitingView stopAnimating];
}

-(IBAction)backClick:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
@end
